package com.shangguigu.crud.test;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.github.pagehelper.PageInfo;
import com.shangguigu.crud.bean.Employee;

/**
 * 使用spring提供的测试模块来测试测试请求功能，测试crud的正确性。
 * @author 凌熬
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})

public class mvctesy {
	private MockMvc  mockmvc;
	//传入springmvc的ioc @WebAppConfiguration可以用获取webIOC的溶解
	@Autowired
	 WebApplicationContext context;
	//虚拟mvc请求，获取处理结果，但是这个mvc需要创建出来 ，才可以使用
	
	@Before
	public void initMokcMvc() {
		  mockmvc=MockMvcBuilders.webAppContextSetup(context).build();
	}
	
	@Test
	public void testpage() throws Exception {
		//模拟请求，拿到返回值。
		MvcResult result=mockmvc.perform(MockMvcRequestBuilders.get("/empss").param("pn","20")).andReturn();
		//请求成功之后，会在请求域中放pageinfo；我们取出pageinfo进行验证
		MockHttpServletRequest request=result.getRequest();
		PageInfo pi = (PageInfo)request.getAttribute("pageInfo");
		System.out.println(pi+"pi");//null
		System.out.println("当前页面"+pi.getPageNum());
		System.out.println("总页码"+pi.getPages());
		System.out.println("总记录数"+pi.getTotal());
		System.out.println(pi.getEndRow());
		System.out.println(pi.getFirstPage());
		//获取员工数据
		List<Employee> list=pi.getList();
		for (Employee object : list) {
			System.out.println("id:"+object.getEmpId()+"\tname:"+object.getEmpName()+"\tgender"+object.getGender());
		}
		//页码显示
		int[] navigatepageNums = pi.getNavigatepageNums();
		for (int i : navigatepageNums) {
			System.out.print("*"+i+"*");
		}
	}

}
